<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field
        label-width="70"
        label-align="center"
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="rulesobj.rulesuser"
      />

      <van-field
        label-width="70"
        label-align="center"
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="rulesobj.rulespass"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <van-field
      v-model="sms"
      center
      clearable
      label="短信验证码"
      placeholder="请输入短信验证码"
    >
      <!--具名插槽-->
      <template #button>
        <van-button round size="small" type="primary">发送验证码</van-button>
      </template>
      <!--具名插槽:自定义输入框尾部图标-->
      <template #right-icon>
        <van-icon name="good-job-o" />
      </template>
    </van-field>
  </div>
</template>
<script>
// const userhandle = (value) => {
//   let str = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
//   let flag = str.test(value)
//   return flag
// }
export default {

  data () {
    return {
      username: '',
      password: '',
      /*表单校验*/
      rulesobj: {
        rulesuser: [
          { required: true, message: '请填写用户名' },
          //添加pattern:手机号正则校验
          // { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '请填写正确手机号' },
          //添加函数方法校验
          { validator: this.validator, message: '请填写正确手机号' }
        ],
        rulespass: [
          { required: true, message: '请填写密码' }
        ]
      },
      sms: ''

    };
  },
  methods: {
    onSubmit (values) {
      console.log('submit', values);
    },
    //函数校验
    validator (value) {
      let str = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
      let flag = str.test(value)
      return flag
    }
  },
};
</script>
<style lang='less'  scoped>
</style>
